       
        
class FormData:
    time_number = 0
    time_id_row = u''
    time_id_list = {}
    time_list = {}
    comments_list = {}
    form_state = u'invalid'
    error_report = u'ok'
    
    def default(self):
        self.time_number = 0
        self.time_id_row = u''
        self.time_id_list = {}
        self.time_list = {}
        self.comments_list = {}
        self.form_state = u'invalid'
        self.error_report = u'ok'
        
    def generate_error(self, error_number):
        if error_number==1:
            self.error_report = u'ERROR #1: QueryDict KeyError was happend'
            self.form_state = u'invalid'
        if error_number==2:
            self.error_report = u'ERROR #2: ValueError in FormData was happend'
            self.form_state = u'invalid'
        if error_number==3:
            self.error_report = u'ERROR #3: some error was happend'
            self.form_state = u'invalid'
        if error_number==4:
            self.error_report = u'ERROR #4: FormData is out of range'
            self.form_state = u'invalid'
        
    def filling_form(self, form, time_number_key, time_id_list_key, time_id_key, time_key, comments_key, if_comments):
        try:
            self.default()
            self.time_number = int(form.__getitem__(time_number_key))
            self.time_id_row = unicode(form.__getitem__(time_id_list_key))
            for x in xrange(self.time_number):
                time_id = int(form.__getitem__(time_id_key + unicode(x)))
                self.time_id_list[unicode(x)] = time_id
                time = float(form.__getitem__(time_key + unicode(x)))
                self.time_list[unicode(time_id)] = time
                if if_comments==1:
                    comment = unicode(form.__getitem__(comments_key + unicode(x)))
                    self.comments_list[unicode(time_id)] = comment
        except KeyError: 
            self.generate_error(1)
        except ValueError:
            self.generate_error(2)
        else:
            if self.time_number==0:
                self.form_state = u'empty'
            elif self.time_number> 0:
                self.form_state = u'valid'
            elif self.time_number< 0:
                self.generate_error(4)
        
    def delete_time_line(self, time_id):
        try:
            for index in xrange(self.time_number):
                if self.time_id_list[unicode(index)]==time_id:
                    self.time_number-= 1
                    self.time_id_list[unicode(index)] = self.time_id_list[unicode(self.time_number)]
                    del self.time_id_list[unicode(self.time_number)]
                    break
            del self.time_list[unicode(time_id)]
        except KeyError:
            self.generate_error(1)
        else:
            try:
                del self.comments_list[unicode(time_id)]
            except KeyError:
                error = u'some comments are apsent'
            self.time_id_row = u''
            if self.time_number==0 and self.form_state==u'valid':
                self.form_state = u'empty'
            if self.time_number< 0:
                self.form_state = u'invalid'
    
    def generate_time_id_row(self):
        self.time_id_row = u''
        try:
            for index in xrange(self.time_number):
                self.time_id_row+= unicode(self.time_id_list[unicode(index)])
                if index< (self.time_number-1):
                   self.time_id_row+= u','
        except KeyError: 
            self.generate_error(1)
    
    